﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace winS.Unity
{
    /// <summary>
    /// 表示控制器
    /// </summary>
    public abstract class Controller
    {
        protected Stack<ControlUnit> controlUnitList = new Stack<ControlUnit>();

        public virtual Task Initialize()
        {
            return Task.CompletedTask;
        }

        public T AddControlUnit<T>(T controlUnit) where T : ControlUnit
        {
            controlUnit.controller = this;
            controlUnitList.Push(controlUnit);
            return controlUnit;
        }
        public T GetControlUnit<T>() where T : ControlUnit
        {
            return controlUnitList.FirstOrDefault(controlUnit => controlUnit is T) as T;
        }
        public void EnableControlUnit<T>() where T : ControlUnit
        {
            GetControlUnit<T>().Enable();
        }
        public void DisableControlUnit<T>() where T : ControlUnit
        {
            GetControlUnit<T>().Disable();
        }
        public void EnableAllControlUnit()
        {
            foreach (var controlUnit in controlUnitList) controlUnit.Enable();
        }
        public void DisableAllControlUnit()
        {
            foreach (var controlUnit in controlUnitList) controlUnit.Disable();
        }
        public void ReleaseAllControlUnit()
        {
            foreach (var controlUnit in controlUnitList) controlUnit.Release();
        }
    }
}